package main

import (
	"flag"
	"fmt"
	"io"
	"net"
	"os"
)

type Client struct {
	ServerIp   string
	ServerPort int
	Name       string
	conn       net.Conn
	flag       int //当前client模式
}

func NewClient(serverIp string, serverPort int) *Client {
	client := &Client{ServerIp: serverIp, ServerPort: serverPort, flag: 3}
	conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", client.ServerIp, client.ServerPort))
	if err != nil {
		fmt.Println("client dial error: ", err)
		return nil
	}
	client.conn = conn
	return client
}

var serverIp string
var serverPort int

func init() {
	flag.StringVar(&serverIp, "ip", "127.0.0.1", "default ip: 127.0.0.1")
	flag.IntVar(&serverPort, "port", 8080, "default port: 8080")
}

func (this *Client) menu() bool {
	var flag int
	fmt.Println("1:公聊模式")
	fmt.Println("2:私聊模式")
	fmt.Println("3:更新用户名")
	fmt.Println("0:退出")
	fmt.Scanln(&flag)

	if flag >= 0 && flag <= 3 {
		this.flag = flag
		return true
	} else {
		fmt.Println("illegal number")
		return false
	}
}

func (this *Client) DealResponse() {
	io.Copy(os.Stdout, this.conn)
}

// 查询在线用户
func (this *Client) SelectUsers() {
	_, err := this.conn.Write([]byte("who\n"))
	if err != nil {
		fmt.Println("conn Write error: ", err)
		return
	}
}

func (this *Client) UpdateName() bool {
	fmt.Println("please input your name")
	fmt.Scanln(&this.Name)
	sendMsg := "rename|" + this.Name + "\n"
	_, err := this.conn.Write([]byte(sendMsg))
	if err != nil {
		fmt.Println("conn write error: ", err)
		return false
	}
	return true
}

func (this *Client) PublicChat() {
	var chatMsg string
	fmt.Println("please input your message, exit means quit")
	fmt.Scanln(&chatMsg)
	for chatMsg != "exit" {
		if len(chatMsg) != 0 {
			sendMsg := chatMsg + "\n"
			_, err := this.conn.Write([]byte(sendMsg))
			if err != nil {
				fmt.Println("conn write err: ", err)
				break
			}
		}

		chatMsg = ""
		fmt.Println("please input your message, exit means quit")
		fmt.Scanln(&chatMsg)
	}
}
func (this *Client) PrivateChat() {
	var chatMsg string
	var remoteName string

	this.SelectUsers()
	fmt.Println("please input your chat friend name, exit means quit")
	fmt.Scanln(&remoteName)

	for remoteName != "exit" {
		fmt.Println("please input your message, exit means quit")
		fmt.Scanln(&chatMsg)

		for chatMsg != "exit" {
			if len(chatMsg) != 0 {
				sendMsg := "to|" + remoteName + "|" + chatMsg + "\n"
				_, err := this.conn.Write([]byte(sendMsg))
				if err != nil {
					fmt.Println("conn write err: ", err)
					break
				}
			}

			chatMsg = ""
			fmt.Println("please input your message, exit means quit")
			fmt.Scanln(&chatMsg)
		}

		this.SelectUsers()
		fmt.Println("please input your chat friend name, exit means quit")
		fmt.Scanln(&remoteName)
	}
}

func (this *Client) Run() {
	for this.flag != 0 {
		for this.menu() != true {
		}
		switch this.flag {
		case 1:
			this.PublicChat()
			break
		case 2:
			this.PrivateChat()
			break
		case 3:
			this.UpdateName()
			break

		}
	}
}

func main() {
	// 命令行解析
	flag.Parse()

	client := NewClient("127.0.0.1", 8080)
	if client == nil {
		fmt.Println("connect to server failed")
		return
	}
	fmt.Println("connect to server succeed")

	//单独开启一个goroutine处理服务器回复消息
	go client.DealResponse()
	client.Run()
}
